package org.dromara.assets.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.assets.biz.AssetsTradeRecordServiceBiz;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.assets.domain.vo.AssetsTradeRecordVo;
import org.dromara.assets.domain.bo.AssetsTradeRecordBo;
import org.dromara.assets.service.IAssetsTradeRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 交易记录
 *
 * @author EdwardChan
 * @date 2025-06-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/assets/traderecord")
public class AssetsTradeRecordController extends BaseController {

    private final IAssetsTradeRecordService assetsTradeRecordService;
    private final AssetsTradeRecordServiceBiz assetsTradeRecordServiceBiz;

    /**
     * 查询交易记录列表
     */
    @SaCheckPermission("assets:traderecord:list")
    @GetMapping("/list")
    public TableDataInfo<AssetsTradeRecordVo> list(AssetsTradeRecordBo bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("id");
        pageQuery.setIsAsc("desc");
        return assetsTradeRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出交易记录列表
     */
    @SaCheckPermission("assets:traderecord:export")
    @Log(title = "交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AssetsTradeRecordBo bo, HttpServletResponse response) {
        List<AssetsTradeRecordVo> list = assetsTradeRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "交易记录", AssetsTradeRecordVo.class, response);
    }

    /**
     * 获取交易记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("assets:traderecord:query")
    @GetMapping("/{id}")
    public R<AssetsTradeRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(assetsTradeRecordService.queryById(id));
    }

    /**
     * 新增交易记录
     */
    @SaCheckPermission("assets:traderecord:add")
    @Log(title = "交易记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AssetsTradeRecordBo bo) {
        return toAjax(assetsTradeRecordService.insertByBo(bo));
    }

    /**
     * 修改交易记录
     */
    @SaCheckPermission("assets:traderecord:edit")
    @Log(title = "交易记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AssetsTradeRecordBo bo) {
        return toAjax(assetsTradeRecordService.updateByBo(bo));
    }

    /**
     * 删除交易记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("assets:traderecord:remove")
    @Log(title = "交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(assetsTradeRecordService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 平仓交易
     *
     * @param ids 主键串
     */
    @SaCheckPermission("assets:traderecord:close")
    //@Log(title = "交易记录", businessType = BusinessType.DELETE)
    @PostMapping("/close/{ids}")
    public R<Void> close(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return assetsTradeRecordServiceBiz.close(ids);
    }
}
